{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 格式化字符串"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "1. 字符串之间也可以进行加法运算，如果将两个字符串进行相加，则会自动将两个字符串拼接为一个"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 'Hello' + ' ' + 'World'\n",
    "print('a = ' + a)# 不常用"
   ]
  },
  {
   "source": [
    "2. 字符串不能和其它类型进行加法运算，如果做了会出现异常 `TypeError: can only concatenate str (not \"int\") to str`"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 'str' + 123"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1\n",
    "print('a =' , a)# 常用"
   ]
  },
  {
   "source": [
    "3. 创建字符串时，可以在字符串中指定占位符\n",
    "    - `%s` - 表示任意字符\n",
    "    - `%f` - 浮点占位符\n",
    "    - `%d` - 整数占位符"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 'a = %s'%'字符串'\n",
    "b = 'b = %s %s'%('Hello','World')\n",
    "c = 'c = %5s'%'字符串'\n",
    "d = 'd = %1.2s'%'字符串'\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 'a = %f'%100.233\n",
    "b = 'a = %.2f'%100.239\n",
    "c = 'a = %7.2f'%100.239\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 'a = %d'%100.233\n",
    "b = 'a = %4d'%100.233\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "source": [
    "4. 可以在字符串前面添加一个 `f` 来创建一个格式化字符串，格式化字符串中可以直接嵌入变量"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 'str'\n",
    "b = 123\n",
    "c = f'a = {a},b = {b}'\n",
    "print(c)"
   ]
  },
  {
   "source": [
    "## 练习"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_name = 'icekylin'\n",
    "print('My name is ' + my_name + '.')\n",
    "print('My name is' , my_name)\n",
    "print('My name is %s.'%my_name)\n",
    "print(f'My name is {my_name}.')"
   ]
  }
 ]
}